package com.jl.obulibrary.sdk;


import android.util.Log;

import com.jl.obulibrary.bean.CarAllBean;
import com.jl.obulibrary.bean.CardBean;
import com.jl.obulibrary.bean.GetSecureBean;
import com.jl.obulibrary.bean.ObuBean;
import com.jl.obulibrary.exceptions.OperationBySerialPortException;
import com.jl.obulibrary.servive.OperationBySerialPort;


import com.jl.obulibrary.operate.OfflineOperate;
import com.jl.obulibrary.utils.HexUtil;
import com.jxhc.rsudriver.model.GetSecure_res;
import com.jxhc.rsudriver.model.INITIALISATION_res;
import com.jxhc.rsudriver.model.TransferChannel_res;

public class SDK_RSU {
    static String TAG = "大智测试SDK_RSU";
    /**
     * 发送BST
     */
    public static void SendBST() throws Exception {


            INITIALISATION_res initialisation_res = OperationBySerialPort.getInstance().JL_CustomBst();
            String application = initialisation_res.getApplication();
            Log.e(TAG, "VST数据为:" + application);
            String strVst = application.substring(2);
            byte[] resultData = HexUtil.decodeHex(strVst.toCharArray());
            if (OfflineOperate.VstResolve(strVst,resultData, (byte) resultData.length) != 0) {
                throw new NumberFormatException("VST信息错误！");
            }



    }

    /**
     * 发送BST过滤MAC 未实现
     */
    public static void SendBSTMAC(String MAC) {

        try {
            OperationBySerialPort.getInstance().JL_BstByMac(MAC);


        } catch (OperationBySerialPortException e) {
            e.printStackTrace();
        }

    }

    /**
     * 发送BST过滤SN 未实现
     */
    public static void SendBSTSN(String SN) {

        try {
            OperationBySerialPort.getInstance().JL_BstBySn(SN);


        } catch (OperationBySerialPortException e) {
            e.printStackTrace();
        }

    }

    /**
     * 获取车辆信息
     * @throws Exception
     */
    public static void OffGetSecure() throws Exception {
        OfflineOperate.JLOffGetSecure();
    }
    /**
     * 获取   OBU信息
     * @throws Exception
     */
    public static CardBean getCardBean() throws Exception {
        CardBean obuBean = OfflineOperate.getCardBean();
        return obuBean;
    }
    /**
     * 获取   OBU信息
     * @throws Exception
     */
    public static ObuBean getObuBean() throws Exception {
        ObuBean obuBean = OfflineOperate.getObuBean();
        return obuBean;
    }
    /**
     * 获取   车辆信息信息
     * @throws Exception
     */
    public static CarAllBean getCarAllBean() throws Exception {
        CarAllBean obuBean = OfflineOperate.getCarAllBean();
        return obuBean;
    }
    /**
     * @param vehicleLen 车辆信息长度
     */
    public static GetSecureBean getSecure(int vehicleLen, String strRandom) {
        GetSecureBean getSecureBean = new GetSecureBean();
        String message = "";

        GetSecure_res getSecure_res = OperationBySerialPort.getInstance().JL_GetSecure(vehicleLen, strRandom);
        if (getSecure_res.rtnValue == 0) {
            message = "成功";
            getSecureBean.setCode(0);
            getSecureBean.setFile(getSecure_res.getFile()+getSecure_res.getAuthenticator());
        } else if (getSecure_res.rtnValue == 100) {
            message = "设备无响应";
            getSecureBean.setCode(1);
        } else if (getSecure_res.rtnValue == 101) {
            message = "超时，OBU无响应";
            getSecureBean.setCode(1);
        } else if (getSecure_res.rtnValue == 1000) {
            message = "传入参数错误";
            getSecureBean.setCode(1);
        } else if (getSecure_res.rtnValue == 1001) {
            message = "设备未打开";
            getSecureBean.setCode(1);
        }
        if (getSecure_res.rtnValue == 2000) {
            message = "其他错误";
            getSecureBean.setCode(1);
        }
        getSecureBean.setMessge(message);
//        Log.e("读车辆信息密文", "提示信息:" + message);
//        Log.e("读车辆信息密文", "数据:" + getSecure_res);

        return getSecureBean;

    }

    /**
     * 发送通用指令
     *
     * @param channalID 1为用户卡,2为 OBU
     * @param cos       发送正常通道指令， 00A40000023F00
     */

    public static TransferChannel_res SendTransferChannel(int channalID, String cos) {
        TransferChannel_res transferChannel_res = null;
        try {
              transferChannel_res=OperationBySerialPort.getInstance().JL_TransferChannel(channalID, cos);
        } catch (OperationBySerialPortException e) {
            e.printStackTrace();
        }
            return  transferChannel_res;

    }




    public static TransferChannel_res SendTransferChannelCustom(String data1, String cos) {
        TransferChannel_res transferChannel_res = null;
        try {
            transferChannel_res=OperationBySerialPort.getInstance().JL_TransferChannel_CUSTOM(data1+cos);
        } catch (OperationBySerialPortException e) {
            e.printStackTrace();
        }
        return  transferChannel_res;

    }



    public static void SendSetMMI() {

        try {
            OperationBySerialPort.getInstance().JL_SetMMI();
        } catch (OperationBySerialPortException e) {
            e.printStackTrace();
        }

    }

    public static void SendEVENTREPORT() {
        try {
            OperationBySerialPort.getInstance().JL_EVENT_REPORT();
        } catch (OperationBySerialPortException e) {
            e.printStackTrace();
        }


    }
    /**
     * 读取硬件版本号
     *
     * @throws Exception
     */
    public static String SendReadVersion() throws Exception {
        String s = OfflineOperate.SendReadVersion();
        return s;
    }
    /**
     * 获取车辆信息
     * @throws Exception
     */
    public static void OffPay() throws Exception {
        OfflineOperate.JLOffPay("1");
    }
    /**
     * OBU脱机激活
     * @throws Exception
     */
    public static void OffActive() throws Exception {
        OfflineOperate.JLOffActive("2",null);
    }
    /**
     * OBU脱机激活,需要预检测，传预检测的MAC
     * @throws Exception
     */
    public static void OffActiveOBUMAC(String MAC) throws Exception {
        OfflineOperate.JLOffActive("1",MAC);
    }
}
